package com.battle.service.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.PersonCallRecord;
import com.battle.pojo.entity.PersonWarCase;
import com.battle.pojo.enums.OrganizationTypeEnum;
import com.battle.pojo.excel.OrganizationTreeListExcel;
import com.battle.pojo.param.personCallRecord.*;
import com.battle.pojo.vo.personCallRecord.*;
import com.battle.pojo.dto.personCallRecord.*;
import com.battle.service.repository.in.PersonCallRecordRepository;
import com.battle.service.repository.in.PersonWarCaseRepository;
import com.battle.service.service.in.PersonCallRecordService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：人员出警记录服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class PersonCallRecordServiceImpl implements PersonCallRecordService {

    private final PersonCallRecordRepository personCallRecordRepository;
    private final PersonWarCaseRepository personWarCaseRepository;

    public PersonCallRecordServiceImpl(PersonCallRecordRepository personCallRecordRepository, PersonWarCaseRepository personWarCaseRepository) {
        this.personCallRecordRepository = personCallRecordRepository;
        this.personWarCaseRepository = personWarCaseRepository;
    }


    /**
     * 创建人员出警记录
     *
     * @param param
     */
    @Override
    public void createPersonCallRecord(CreatePersonCallRecordParam param) {
        PersonCallRecord personCallRecord = new PersonCallRecord();
        BeanUtils.copyProperties(param, personCallRecord);
        Long id = IdGenerator.SNOW.generate();
        personCallRecord.setId(id);
        personCallRecordRepository.create(personCallRecord);
    }


    /**
     * 移除人员出警记录
     *
     * @param id
     */
    @Override
    public void removePersonCallRecord(Long id) {
        personCallRecordRepository.removeById(id);
    }

    /**
     * 启动/禁用人员出警记录
     *
     * @param id
     */
    @Override
    public void enabledPersonCallRecord(Long id) {
        PersonCallRecord personCallRecord = personCallRecordRepository.getByIdCheck(id);
        personCallRecord.setIsDisable(!personCallRecord.getIsDisable());
        personCallRecordRepository.edit(personCallRecord);
    }

    /**
     * 获取人员出警记录详情
     *
     * @param id
     * @return
     */
    @Override
    public GetPersonCallRecordVo getPersonCallRecord(Long id) {
        PersonCallRecord personCallRecord = personCallRecordRepository.getByIdCheck(id);
        GetPersonCallRecordVo vo = new GetPersonCallRecordVo();
        BeanUtils.copyProperties(personCallRecord, vo);
        return vo;
    }

    /**
     * 获取人员出警记录 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetPersonCallRecordListVo> getPersonCallRecordList(GetPersonCallRecordListParam param) {

        SelectPersonCallRecordDto dto = new SelectPersonCallRecordDto();
        BeanUtils.copyProperties(param, dto);
        Page<PersonCallRecord> page = personCallRecordRepository.selectPersonCallRecord(dto);
        List<PersonCallRecord> personCallRecordList = page.getRecords();

        List<GetPersonCallRecordListVo> list = new ArrayList<>();
        for (PersonCallRecord personCallRecord : personCallRecordList) {
            GetPersonCallRecordListVo vo = new GetPersonCallRecordListVo();
            BeanUtils.copyProperties(personCallRecord, vo);
            //todo 人员姓名
            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取人员出警记录下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetPersonCallRecordDownListVo> getPersonCallRecordDownList(GetPersonCallRecordDownListParam param) {
        PersonCallRecord po = new PersonCallRecord();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<PersonCallRecord> personCallRecordList = personCallRecordRepository.getListByEntity(po);

        List<GetPersonCallRecordDownListVo> list = new ArrayList<>();
        for (PersonCallRecord personCallRecord : personCallRecordList) {
            GetPersonCallRecordDownListVo vo = new GetPersonCallRecordDownListVo();
            BeanUtils.copyProperties(personCallRecord, vo);
            list.add(vo);
        }
        return list;
    }

    /**
     * 导入出警记录
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importPersonCallRecord(ImportPersonCallRecordParam param) throws IOException {
        PersonWarCase personWarCase = personWarCaseRepository.getByIdCheck(param.getId());
        if (personWarCase == null) {
            throw new BizException("人员名称不存在");
        }

        StringBuffer error = new StringBuffer();

        InputStream inputStream =null;
        try {
            URL url=new URL(param.getFileUrl());
            URLConnection urlConnection = url.openConnection();
            inputStream=urlConnection.getInputStream();
            if (inputStream == null) {
                throw new BizException("文件不存在");
            }

            ImportParams params = new ImportParams();
            params.setTitleRows(0);
            params.setStartRows(0);

            List<EditPersonCallRecordParam> result = ExcelImportUtil.importExcel(inputStream, EditPersonCallRecordParam.class, params);
            for (EditPersonCallRecordParam excel : result) {
                PersonCallRecord personCallRecord = new PersonCallRecord();
                BeanUtils.copyProperties(excel, personCallRecord);
                personCallRecord.setId(IdGenerator.SNOW.generate());
                personCallRecord.setPersonId(personWarCase.getId());
                personCallRecordRepository.save(personCallRecord);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            inputStream.close();
        }

        if (!StringUtils.isEmpty(error.toString())) {
            throw new BizException(error.toString());
        }
    }


}



